package ${package}.controller;

import fun.yizhierha.common.annotation.Log;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.exception.BizCodeEnum;
import fun.yizhierha.common.utils.ValidUtils;
import fun.yizhierha.common.utils.*;
import ${package}.domain.${className};
import ${package}.domain.vo.Create${className}Vo;
import ${package}.domain.vo.Update${className}Vo;
import ${package}.domain.vo.Retrieve${className}Vo;

import ${package}.service.${className}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

/** generated by EH-Admin
* @author ${author}
* @date ${date}
**/
@Api(tags = "${apiAlias}")
@RestController
@RequestMapping("/api/${api_path}")
@RequiredArgsConstructor
public class ${className}Controller{

    private final ${className}Service ${changeClassName}Service;

    @ApiOperation("获取${apiAliasShort}")
    @Log("获取${apiAliasShort}")
    @GetMapping
    @PreAuthorize("@eh.check('${apiPathF}:${apiPathS}:list')")
    public R<PageUtils<${className}>> list(Retrieve${className}Vo retrieve${className}Vo, Query.PageVo pageVo){
        PageUtils<${className}> res = ${changeClassName}Service.list(retrieve${className}Vo,pageVo);
        return R.<PageUtils<${className}>>ok().setData(res);
    }

    @ApiOperation("新增${apiAliasShort}")
    @Log("新增${apiAliasShort}")
    @PostMapping
    @PreAuthorize("@eh.check('${apiPathF}:${apiPathS}:add')")
    public R<List<BaseErrDto>> add(@Validated @RequestBody Create${className}Vo create${className}Vo,
        BindingResult bindingResult){
        List<BaseErrDto> errDtoList = ValidUtils.getBaseErrDtoByBindingRes(bindingResult);
        if (errDtoList.isEmpty()){
            ${changeClassName}Service.save(create${className}Vo);
            return R.ok();
        }
        return R.<List<BaseErrDto>>error(
                BizCodeEnum.Client_Error_CRUD.getCode(), BizCodeEnum.Client_Error_CRUD.getMsg()
        ).setData(errDtoList);
    }

    @ApiOperation("修改${apiAliasShort}")
    @Log("修改${apiAliasShort}")
    @PutMapping
    @PreAuthorize("@eh.check('${apiPathF}:${apiPathS}:edit')")
    public R<List<BaseErrDto>> edit(@Validated @RequestBody ValidList<Update${className}Vo> update${className}List,
                    BindingResult bindingResult){
        List<BaseErrDto> errDtoList = ValidUtils.getBaseErrDtoByBindingRes(update${className}List, bindingResult);
        if (errDtoList.isEmpty()) {
            ${changeClassName}Service.edit(update${className}List,errDtoList);
            if (errDtoList.isEmpty()){
                return R.ok();
            }else {
                return  R.<List<BaseErrDto>>error(
                        BizCodeEnum.Client_Error_CRUD.getCode(),
                        BizCodeEnum.Client_Error_CRUD.getMsg()
                ).setData(errDtoList);
            }
        }
        return  R.<List<BaseErrDto>>error(
                BizCodeEnum.Client_Error_CRUD.getCode(),
                BizCodeEnum.Client_Error_CRUD.getMsg()
        ).setData(errDtoList);
    }

    @ApiOperation("删除${apiAliasShort}")
    @Log("删除${apiAliasShort}")
    @DeleteMapping
    @PreAuthorize("@eh.check('${apiPathF}:${apiPathS}:del')")
    public R del(@RequestBody Set<Long> ids){
        ${changeClassName}Service.remove(ids);
        return R.ok();
    }

    @ApiOperation("导出数据")
    @Log("导出数据")
    @GetMapping("/download")
    @PreAuthorize("@eh.check('${apiPathF}:${apiPathS}:list')")
    public void download(HttpServletResponse response){
        ${changeClassName}Service.download(response);
    }

}